package org.tissou.client.Services;

import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.rpc.AsyncCallback;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Claire
 * Date: 19-Mar-2009
 * Time: 21:54:11
 * To change this template use File | Settings | File Templates.
 */
public class ArbreDeMonnaies extends Tree{

  private AsyncCallback<List<MonnaieInfos>> chargeageCallback = new AsyncCallback<List<MonnaieInfos>>() {

    public void onFailure(Throwable caught) {
      addItem("Erreur mec. Arbre monnaie non charge." + caught.getMessage());
    }

    public void onSuccess(List<MonnaieInfos> result) {
      for (MonnaieInfos m : result){
        TreeItem t = new TreeItem(m.getNomMonnaie());
        t.setUserObject(m);
        addItem(t);
      }
    }
  };

  public ArbreDeMonnaies() {
    super();
    chargerLEnsembleDesMonnaies();

  }

  private void chargerLEnsembleDesMonnaies() {
    EnsembleMonnaies.App.getInstance().donnerListeMonnaies(chargeageCallback);
  }

    public MonnaieInfos donneLaMonnaieSelectionnee() throws MonException{
    try {
      return (MonnaieInfos) this.getSelectedItem().getUserObject();
    } catch (NullPointerException e){
      throw new MonException("Tu n'as pas selectionne de monnaie !", e);
    }
  }


}